﻿using UnityEngine;
using System.Collections;

namespace GameBasic
{
    /// <summary>
    /// float mask
    /// </summary>
    public struct FloatMask
    {
        public uint value;

        public FloatMask(uint v)
        {
            value = v;
        }

        public FloatMask(float v)
        {
            this.value = 1u << ((int)(v * 32) & 31);
        }

        /// <summary>
        /// Range Mask
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        public FloatMask(float min, float max)
        {
            this.value = Range(min, max);
        }

        public void SetOn(float value)
        {
            this.value |= 1u << ((int)(value * 32) & 31);
        }

        public void SetOff(float value)
        {
            this.value &= ~(1u << ((int)(value * 32) & 31));
        }

        public bool IsOn(float value)
        {
            return (this.value & GetMask(value)) != 0;
        }

        public static uint GetMask(float value)
        {
            return 1u << ((int)(value * 32) & 31);
        }

        public static uint Range(float min, float max)
        {
            // min
            uint u0 = uint.MaxValue << ((int)(min * 32) & 31);
            // max
            uint u1 = uint.MaxValue >> ((int)((1 - max) * 32) & 31);
            return u0 & u1;
        }

        public static uint RangeMin(float min)
        {
            return uint.MaxValue << ((int)(min * 32) & 31);
        }

        public static uint RangeMax(float max)
        {
            return uint.MaxValue >> ((int)((1 - max) * 32) & 31);
        }
    }
}